/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.business.service;

import han.bifang.business.dao.BusServiceRelDao;
import han.bifang.business.dao.MappingServiceDao;
import han.bifang.business.dao.MappingServiceVersionDao;
import han.bifang.business.dao.MethodDao;
import han.bifang.business.dao.MethodParamDao;
import han.bifang.business.dao.MethodQosDao;
import han.bifang.business.dao.ServiceBindingDao;
import han.bifang.business.dao.ServiceDao;
import han.bifang.business.dao.ServiceQosDao;
import han.bifang.business.dao.ServiceVersionDao;
import han.bifang.business.dao.StatServiceDao;
import han.bifang.business.dao.VersionQosDao;
import han.bifang.business.dao.VersionSchemaDao;
import han.bifang.business.dao.WsdlDao;
import han.bifang.business.sec.SecurityManager;
import han.bifang.business.sync.SyncUtils;
import han.bifang.common.entity.EntityConstants;
import han.bifang.common.entity.SoaBusServiceRel;
import han.bifang.common.entity.SoaBusServiceRelPK;
import han.bifang.common.entity.SoaHttpBinding;
import han.bifang.common.entity.SoaJmsBindingWrapper;
import han.bifang.common.entity.SoaMappingService;
import han.bifang.common.entity.SoaMappingServiceVersion;
import han.bifang.common.entity.SoaMethodQos;
import han.bifang.common.entity.SoaResource;
import han.bifang.common.entity.SoaService;
import han.bifang.common.entity.SoaServiceQos;
import han.bifang.common.entity.SoaServiceVersion;
import han.bifang.common.entity.SoaSoapBinding;
import han.bifang.common.entity.SoaStatService;
import han.bifang.common.entity.SoaVersionQos;
import han.bifang.common.entity.SoaVersionSchema;
import han.bifang.common.entity.SoaWebServicesBinding;
import han.bifang.common.entity.SoaWebServicesMethod;
import han.bifang.common.entity.SoaWebServicesMethodWrapper;
import han.bifang.common.entity.SoaWsdlWrapper;
import han.bifang.common.exception.BifangErrorCode;
import han.bifang.common.exception.BifangException;
import han.bifang.common.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @Author Kent
 */
public class ServiceManager {

	private static Log logger = LogFactory.getLog(ServiceManager.class);

	public static String createService(SoaService s,
			ArrayList<SoaServiceQos> q, String busId) throws BifangException {
		try {
			// s.setStatus(EntityConstants.SERVICE_STATUS_UNPUBLISHED);
			s.setCreateTime(CommonUtils.getNowStr());

			ServiceDao.createService(s);
			for (SoaServiceQos qs : q) {
				qs.setServiceId(s.getId());
			}
			ServiceQosDao.createServiceQos(q);
			if (busId != null) {
				SoaBusServiceRel rel = new SoaBusServiceRel();
				SoaBusServiceRelPK pk = new SoaBusServiceRelPK();
				pk.setBusId(busId);
				pk.setServiceId(s.getId());
				rel.setCompId(pk);
				BusServiceRelDao.createBusServiceRef(rel);

			}

			// create resource
			SoaResource r = new SoaResource();
			r.setEntityId(s.getId());
			r.setType(EntityConstants.RESOURCE_LOCAL_SERVICE);
			r.setProperty1(s.getServiceName());
			r.setProperty2(s.getServiceCode());

			SecurityManager.createResource(r);

			return s.getId();
		} catch (BifangException e) {
			throw e;

		}

	}

	public static void modifyService(SoaService s) throws BifangException {
		try {
			// s.setStatus(EntityConstants.SERVICE_STATUS_UNPUBLISHED);
			// s.setCreateTime(new Date().toString());
			ServiceDao.modifyService(s);

			SoaResource r = SecurityManager.getResourceByEntity(s.getId(),
					EntityConstants.RESOURCE_LOCAL_SERVICE);
			r.setProperty1(s.getServiceName());
			r.setProperty2(s.getServiceCode());
			
			SecurityManager.modifyResource(r);
			return;
		} catch (BifangException e) {
			throw e;

		}

	}

	public static void modifyServiceQos(List s) throws BifangException {
		try {
			// s.setStatus(EntityConstants.SERVICE_STATUS_UNPUBLISHED);
			// s.setCreateTime(new Date().toString());
			ServiceQosDao.modifyServiceQos(s);

			return;
		} catch (BifangException e) {
			throw e;

		}

	}

	public static void modifyBusServiceRel(SoaBusServiceRel s)
			throws BifangException {
		try {
			// s.setStatus(EntityConstants.SERVICE_STATUS_UNPUBLISHED);
			// s.setCreateTime(new Date().toString());
			BusServiceRelDao.modifyBusServiceRef(s);

			return;
		} catch (BifangException e) {
			throw e;

		}

	}

	public static void modifyRemoteBusServiceRel(List<SoaBusServiceRel> s)
			throws BifangException {
		try {
			// s.setStatus(EntityConstants.SERVICE_STATUS_UNPUBLISHED);
			// s.setCreateTime(new Date().toString());
			BusServiceRelDao.modifyRemoteBusServiceRef(s);

			return;
		} catch (BifangException e) {
			throw e;

		}

	}

	public static List getServiceVersionByServiceId(String serId)
			throws BifangException {
		try {
			return ServiceVersionDao.getServiceVersionByServiceId(serId);
		} catch (BifangException e) {
			throw e;

		}

	}

	public static List getRemoteServiceVersionByServiceId(String serId)
			throws BifangException {
		try {
			return MappingServiceVersionDao.getVersionByServiceId(serId);
		} catch (BifangException e) {
			throw e;

		}

	}

	public static ArrayList getServiceDetail(String id) throws BifangException {
		try {
			ArrayList ret = new ArrayList();
			SoaService ser = ServiceDao.getService(id);
			List qos = ServiceQosDao.getServiceQosByServiceId(id);
			List busrel = BusServiceRelDao.getBusServiceRefByServiceId(id);
			List versionList = ServiceVersionDao.getVersionListByServiceId(id);

			ret.add(ser);
			ret.add(qos);
			ret.add(busrel);
			ret.add(versionList);
			return ret;

		} catch (BifangException e) {
			throw e;

		}

	}

	public static ArrayList searchService(String hql, int page, int size)
			throws BifangException {
		try {

			return ServiceDao.searchService(hql, page, size);

		} catch (BifangException e) {
			throw e;

		}

	}

	public static ArrayList searchMappingService(String hql, int page, int size)
			throws BifangException {
		try {

			return MappingServiceDao.searchService(hql, page, size);

		} catch (BifangException e) {
			throw e;

		}

	}

	public static String createVersion(SoaServiceVersion s, Object binding,
			List versionQos) throws BifangException {
		try {
			s.setPublishTime(CommonUtils.getNowStr());
			if (binding instanceof SoaHttpBinding) {
				ServiceBindingDao.createBinding(binding);
				s.setBindingId(((SoaHttpBinding) binding).getId());
				ServiceVersionDao.createVersion(s);

			}
			if (binding instanceof SoaSoapBinding) {
				ServiceBindingDao.createBinding(binding);
				s.setBindingId(((SoaSoapBinding) binding).getId());
				ServiceVersionDao.createVersion(s);

			} else if (binding instanceof SoaJmsBindingWrapper) {
				SoaJmsBindingWrapper w = (SoaJmsBindingWrapper) binding;

				ServiceBindingDao.createBinding(binding);
				s.setBindingId(w.getJmsBinding().getId());
				ServiceVersionDao.createVersion(s);

			} else if (binding instanceof SoaWsdlWrapper) {
				SoaWsdlWrapper w = (SoaWsdlWrapper) binding;
				SoaWebServicesBinding b = w.getWebServicesBinding();
				ServiceBindingDao.createBinding(b);
				s.setBindingId(b.getId());
				ServiceVersionDao.createVersion(s);
				w.getWsdl().setVersionId(s.getId());
				WsdlDao.createWsdl(w.getWsdl());
				ArrayList l = w.getMethods();
				for (int i = 0, j = l.size(); i < j; i++) {
					SoaWebServicesMethodWrapper mw = (SoaWebServicesMethodWrapper) l
							.get(i);
					mw.getMethod().setVersionId(s.getId());
					MethodDao.createWebServicesMethod(mw.getMethod());
					 mw.getParams().setServiceMethodId(mw.getMethod().getId());
					 MethodParamDao.createMethodParams(mw.getParams());
					List ll = mw.getQos();
					for (int ii = 0, jj = ll.size(); ii < jj; ii++) {
						SoaMethodQos q = (SoaMethodQos) ll.get(ii);
						q.setMethodId(mw.getMethod().getId());

					}
					MethodQosDao.createMethodQos(ll);
				}

			}
			for (int i = 0, j = versionQos.size(); i < j; i++) {
				SoaVersionQos q = (SoaVersionQos) versionQos.get(i);
				q.setVersionId(s.getId());
			}
			VersionQosDao.createVersionQos(versionQos);
			if (s.isDefault()) {
				ServiceDao.setDefaultVersion(s.getServiceId(), s.getVersion());
			}
			
			if(!s.getBindingType().equals(EntityConstants.BINDING_WEB_SERVICES)){
				SoaVersionSchema vs = new SoaVersionSchema();
				vs.setVersionId(s.getId());
				VersionSchemaDao.createVersionSchema(vs);
			}
			
			SoaStatService sss = new SoaStatService();
			
			sss.setMaxResponseTime(0);
			sss.setMinResponseTime(0);
			sss.setServiceCode(s.getServiceCode());
			sss.setServiceId(s.getServiceId());
			sss.setStatBeginTime(String.valueOf(System.currentTimeMillis()));
			sss.setSuccessRequest(0);
			sss.setTotalRequest(0);
			sss.setTotalResponseTime(0);
			sss.setVersion(s.getVersion());
			
			StatServiceDao.createStatService(sss);
			
			return s.getId();

		} catch (BifangException e) {
			throw e;

		}

	}

	public static void modifVersion(SoaServiceVersion s, Object binding,
			List versionQos) throws BifangException {
		try {
			if (s.isDefault()) {
				ServiceDao.setDefaultVersion(s.getServiceId(), s.getVersion());
			} else {
				ServiceDao.setDefaultVersion(s.getServiceId(), null);
			}
			// delete old binding,version qos, mehtod, param, methodqos
			SoaServiceVersion old = ServiceVersionDao.getServiceVersionById(s
					.getId());
			if(old.getBindingType().equals(EntityConstants.BINDING_WEB_SERVICES)
					&& !s.getBindingType().equals(EntityConstants.BINDING_WEB_SERVICES)){
				WsdlDao.removeWsdlByVersionId(s.getId());
				SoaVersionSchema vs = new SoaVersionSchema();
				vs.setVersionId(s.getId());
				VersionSchemaDao.createVersionSchema(vs);
			}else if(! old.getBindingType().equals(EntityConstants.BINDING_WEB_SERVICES)
					&& s.getBindingType().equals(EntityConstants.BINDING_WEB_SERVICES)){
				VersionSchemaDao.removeVersionSchema(s.getId());
			}
			
			

			VersionQosDao.removeVersionQosByVersionId(s.getId());
			if (old.getBindingType().equals(EntityConstants.BINDING_HTTP)) {
				ServiceBindingDao.removeHttpBinding(old.getBindingId());
			}
			if (old.getBindingType().equals(EntityConstants.BINDING_SOAP)) {
				ServiceBindingDao.removeSoapBinding(old.getBindingId());
			} else if (old.getBindingType().equals(EntityConstants.BINDING_JMS)) {
				ServiceBindingDao.removeJmsBinding(old.getBindingId());
			} else if (old.getBindingType().equals(
					EntityConstants.BINDING_WEB_SERVICES)) {
				List l = MethodDao.getWebServicesMethodByVersionId(s.getId());
				for (int i = 0, j = l.size(); i < j; i++) {
					SoaWebServicesMethod m = (SoaWebServicesMethod) l.get(i);
					 MethodParamDao.removeMethodParamsByMethodId(m.getId());
					MethodQosDao.removeMethodQosByMethodId(m.getId());
				}
				MethodDao.removeWebServicesMethodByVersionId(s.getId());
				WsdlDao.removeWsdlByVersionId(s.getId());
				ServiceBindingDao.removeWebServicesBinding(s.getId());

			}

			if (binding instanceof SoaHttpBinding) {
				ServiceBindingDao.createBinding(binding);
				s.setBindingId(((SoaHttpBinding) binding).getId());
				ServiceVersionDao.modifyVersion(s);

			}
			if (binding instanceof SoaSoapBinding) {
				ServiceBindingDao.createBinding(binding);
				s.setBindingId(((SoaSoapBinding) binding).getId());
				ServiceVersionDao.modifyVersion(s);

			} else if (binding instanceof SoaJmsBindingWrapper) {
				SoaJmsBindingWrapper w = (SoaJmsBindingWrapper) binding;
				ServiceBindingDao.createBinding(binding);
				s.setBindingId(w.getJmsBinding().getId());
				ServiceVersionDao.modifyVersion(s);

			} else if (binding instanceof SoaWsdlWrapper) {
				SoaWsdlWrapper w = (SoaWsdlWrapper) binding;
				SoaWebServicesBinding b = w.getWebServicesBinding();
				ServiceBindingDao.createBinding(b);
				s.setBindingId(b.getId());
				ServiceVersionDao.modifyVersion(s);

				w.getWsdl().setId(s.getId());
				w.getWsdl().setVersionId(s.getId());
				WsdlDao.createWsdl(w.getWsdl());

				ArrayList l = w.getMethods();
				for (int i = 0, j = l.size(); i < j; i++) {
					SoaWebServicesMethodWrapper mw = (SoaWebServicesMethodWrapper) l
							.get(i);
					mw.getMethod().setVersionId(s.getId());
					MethodDao.createWebServicesMethod(mw.getMethod());
					 mw.getParams().setServiceMethodId(mw.getMethod().getId());
					 MethodParamDao.createMethodParams(mw.getParams());
					List ll = mw.getQos();
					for (int ii = 0, jj = ll.size(); ii < jj; ii++) {
						SoaMethodQos q = (SoaMethodQos) ll.get(ii);
						q.setMethodId(mw.getMethod().getId());

					}
					MethodQosDao.createMethodQos(ll);
				}

			}
			for (int i = 0, j = versionQos.size(); i < j; i++) {
				SoaVersionQos q = (SoaVersionQos) versionQos.get(i);
				q.setVersionId(s.getId());
			}
			VersionQosDao.createVersionQos(versionQos);
			SyncUtils.sync(SyncUtils.SERVICE, s.getServiceCode());
		} catch (BifangException e) {
			throw e;

		}

	}

	public static void removeVersion(String id) throws BifangException {
		try {
			// delete old binding,version qos, mehtod, param, methodqos
			SoaServiceVersion old = ServiceVersionDao.getServiceVersionById(id);

			VersionQosDao.removeVersionQosByVersionId(id);
			if (old.getBindingType().equals(EntityConstants.BINDING_HTTP)) {
				ServiceBindingDao.removeHttpBinding(old.getBindingId());
			} else if (old.getBindingType()
					.equals(EntityConstants.BINDING_SOAP)) {
				ServiceBindingDao.removeSoapBinding(old.getBindingId());
			} else if (old.getBindingType().equals(EntityConstants.BINDING_JMS)) {
				ServiceBindingDao.removeJmsBinding(old.getBindingId());
			} else if (old.getBindingType().equals(
					EntityConstants.BINDING_WEB_SERVICES)) {
				List l = MethodDao.getWebServicesMethodByVersionId(id);
				for (int i = 0, j = l.size(); i < j; i++) {
					SoaWebServicesMethod m = (SoaWebServicesMethod) l.get(i);
					 MethodParamDao.removeMethodParamsByMethodId(m.getId());
					MethodQosDao.removeMethodQosByMethodId(m.getId());
				}
				MethodDao.removeWebServicesMethodByVersionId(id);
				ServiceBindingDao.removeWebServicesBinding(id);
				WsdlDao.removeWsdlByVersionId(id);
			}
			VersionSchemaDao.removeVersionSchema(id);
			ServiceVersionDao.removeVersion(old);
			StatServiceDao.removeStatService(old.getServiceCode(), old.getVersion());
			SyncUtils.sync(SyncUtils.SERVICE, old.getServiceCode());
		} catch (BifangException e) {
			throw e;

		}

	}

	public static void removeService(String id) throws BifangException {
		try {
			// delete old binding,version qos, mehtod, param, methodqos
			SoaService ss = ServiceDao.getService(id);
			List l = ServiceVersionDao.getServiceVersionByServiceId(id);
			if (l.size() > 0) {
				throw new BifangException(BifangErrorCode.HAS_VERSION_ERROR,
						"this service has versions, remove the version first");
			}
			ServiceDao.removeService(id);
			BusServiceRelDao.removeBusServiceRefByServiceId(id);
			ServiceQosDao.removeServiceQosByServiceId(id);
			
			SecurityManager.removeResourceByEntity(id, EntityConstants.RESOURCE_LOCAL_SERVICE);
			if(ss != null)
				SyncUtils.sync(SyncUtils.SERVICE,ss.getServiceCode() );
		} catch (BifangException e) {
			throw e;

		}

	}

	public static ArrayList getVersionDetail(String id) throws BifangException {
		try {
			ArrayList ret = new ArrayList();

			SoaServiceVersion v = ServiceVersionDao.getServiceVersionById(id);
			SoaService s = ServiceDao.getService(v.getServiceId());
			if (v.getVersion().equals(s.getDefaultVersion())) {
				v.setDefault(true);
			}
			List qos = VersionQosDao.getVersionQosByVersionId(id);
			Object b = null;
			if (v.getBindingType().equals(EntityConstants.BINDING_HTTP)) {
				b = ServiceBindingDao.getHttpBinding(v.getBindingId());
			} else if (v.getBindingType().equals(EntityConstants.BINDING_SOAP)) {
				b = ServiceBindingDao.getSoapBinding(v.getBindingId());
			} else if (v.getBindingType().equals(EntityConstants.BINDING_JMS)) {
				b = ServiceBindingDao.getJmsBinding(v.getBindingId());
			} else if (v.getBindingType().equals(
					EntityConstants.BINDING_WEB_SERVICES)) {
				SoaWsdlWrapper sww = new SoaWsdlWrapper();

				SoaWebServicesBinding binding = ServiceBindingDao
						.getWebServicesBinding(v.getBindingId());
				sww.setWebServicesBinding(binding);

				sww.setWsdl(WsdlDao.findWsdlByVersionId(id));

				List methods = MethodDao.getWebServicesMethodByVersionId(v
						.getId());
				ArrayList mw = new ArrayList();
				for (int i = 0, j = methods.size(); i < j; i++) {
					SoaWebServicesMethod swm = (SoaWebServicesMethod) methods
							.get(i);
					SoaWebServicesMethodWrapper swmw = new SoaWebServicesMethodWrapper();
					swmw.setMethod(swm);
					// swmw.setParams(MethodParamDao.getMethodParamsByMethodId(swm
					// .getId()));
					swmw.setQos(MethodQosDao
							.getMethodQosByMethodId(swm.getId()));
					mw.add(swmw);
				}
				sww.setMethods(mw);
				b = sww;
			}

			ret.add(v);
			ret.add(b);
			ret.add(qos);
			return ret;

		} catch (BifangException e) {
			throw e;

		}
	}

	public static ArrayList getMappingServiceVersionDetail(String id)
			throws BifangException {
		try {
			ArrayList ret = new ArrayList();

			SoaMappingServiceVersion v = MappingServiceVersionDao
					.getVersion(id);
			List qos = VersionQosDao.getVersionQosByVersionId(id);

			ret.add(v);
			ret.add(qos);
			return ret;

		} catch (BifangException e) {
			throw e;

		}
	}

	public static String createMappingService(SoaMappingService s,
			ArrayList<SoaServiceQos> qos, ArrayList<String> busRef)
			throws BifangException {
		try {
			MappingServiceDao.createService(s);
			for (SoaServiceQos sq : qos) {
				sq.setServiceId(s.getId());
			}

			ServiceQosDao.createServiceQos(qos);
			ArrayList<SoaBusServiceRel> tmp = new ArrayList<SoaBusServiceRel>();
			for (int i = 0, j = busRef.size(); i < j; i++) {
				String r = busRef.get(i);
				SoaBusServiceRel a = new SoaBusServiceRel();
				SoaBusServiceRelPK k = new SoaBusServiceRelPK();
				k.setBusId(r);
				k.setServiceId(s.getId());
				a.setCompId(k);
				tmp.add(a);

			}
			BusServiceRelDao.createBusServiceRef(tmp);
			
			SoaResource r = new SoaResource();
			r.setEntityId(s.getId());
			r.setType(EntityConstants.RESOURCE_REMOTE_SERVICE);
			r.setProperty1(s.getName());
			r.setProperty2(s.getLocalCode());
			r.setProperty3(s.getRemoteCode());
			
			SecurityManager.createResource(r);
			return s.getId();

		} catch (BifangException e) {
			throw e;

		}

	}

	public static void modifyMappingService(SoaMappingService s)
			throws BifangException {
		try {
			MappingServiceDao.modifyService(s);
			// BusServiceRelDao.removeBusServiceRefByServiceId(s.getId());
			// ArrayList tmp = new ArrayList();
			// for(int i = 0 , j = busRef.size(); i < j ; i ++){
			// SoaBusServiceRel r = new SoaBusServiceRel();
			// SoaBusServiceRelPK p = new SoaBusServiceRelPK();
			// p.setBusId((String)busRef.get(i));
			// p.setServiceId(s.getId());
			// r.setCompId(p);
			// tmp.add(r);
			// }
			//			
			// BusServiceRelDao.createBusServiceRef(tmp);
			SoaResource r = SecurityManager.getResourceByEntity(s.getId(), EntityConstants.RESOURCE_REMOTE_SERVICE);
			r.setProperty1(s.getName());
			r.setProperty2(s.getLocalCode());
			r.setProperty3(s.getRemoteCode());
			SecurityManager.modifyResource(r);
			SyncUtils.sync(SyncUtils.MAPPING_SERVICE, s.getLocalCode());
		} catch (BifangException e) {
			throw e;

		}

	}

	public static ArrayList getMappingService(String id) throws BifangException {
		try {
			ArrayList ret = new ArrayList();
			ret.add(MappingServiceDao.getMappingService(id));
			List qos = ServiceQosDao.getServiceQosByServiceId(id);

			List r = BusServiceRelDao.getBusServiceRefByServiceId(id);
			// List v = MappingServiceVersionDao.getVersionByServiceId(id);
			ret.add(qos);
			ret.add(r);

			// ret.add(v);
			return ret;

		} catch (BifangException e) {
			throw e;

		}

	}

	public static void removeMappingService(String id) throws BifangException {
		try {
			SoaMappingService s = MappingServiceDao.getMappingService(id);
			MappingServiceDao.removeService(id);
			ServiceQosDao.removeServiceQosByServiceId(id);
			BusServiceRelDao.removeBusServiceRefByServiceId(id);
			MappingServiceVersionDao.removeVersionByServiceId(id);
			
			SecurityManager.removeResourceByEntity(id, EntityConstants.RESOURCE_REMOTE_SERVICE);
			if(s != null)
			SyncUtils.sync(SyncUtils.MAPPING_SERVICE, s.getLocalCode());
		} catch (BifangException e) {
			throw e;

		}

	}

	public static String createMappingServiceVersion(
			SoaMappingServiceVersion s, ArrayList<SoaVersionQos> qos)
			throws BifangException {
		try {
			MappingServiceVersionDao.createVersion(s);
			for (SoaVersionQos q : qos) {
				q.setVersionId(s.getId());
			}
			VersionQosDao.createVersionQos(qos);
			SoaVersionSchema vs = new SoaVersionSchema();
			vs.setVersionId(s.getId());
			VersionSchemaDao.createVersionSchema(vs);
			return s.getId();
		} catch (BifangException e) {
			throw e;

		}

	}

	public static void removeMappingServiceVersion(String id)
			throws BifangException {
		try {
			SoaMappingServiceVersion s = MappingServiceVersionDao.getVersion(id);
			
			MappingServiceVersionDao.removeVersion(id);
			VersionQosDao.removeVersionQosByVersionId(id);
			VersionSchemaDao.removeVersionSchema(id);
			if(s != null){
				SyncUtils.sync(SyncUtils.MAPPING_SERVICE, s.getLocalCode());
			}
			
		} catch (BifangException e) {
			throw e;

		}

	}

	public static void modifyMappingServiceVersion(SoaMappingServiceVersion s,
			ArrayList<SoaVersionQos> versionQos) throws BifangException {
		try {

			MappingServiceVersionDao.modifyVersion(s);
			VersionQosDao.modifyVersionQos(versionQos);

			// for (int i = 0, j = versionQos.size(); i < j; i++) {
			// SoaVersionQos q = (SoaVersionQos) versionQos.get(i);
			// q.setVersionId(s.getId());
			// }
			// VersionQosDao.createVersionQos(versionQos);
			SyncUtils.sync(SyncUtils.MAPPING_SERVICE, s.getLocalCode());

		} catch (BifangException e) {
			throw e;

		}

	}
	
	public static void modifyVersionSchema(SoaVersionSchema vs)throws BifangException {
		try {
			if(vs != null && vs.getVersionId() != null)
				VersionSchemaDao.removeVersionSchema(vs.getVersionId());
			VersionSchemaDao.createVersionSchema(vs);

		} catch (BifangException e) {
			throw e;

		}

	}
	
	public static SoaVersionSchema getVersionSchema(String id)throws BifangException {
		try {

			return VersionSchemaDao.getVersionSchema(id);

		} catch (BifangException e) {
			throw e;

		}

	}
	
	public static boolean canRetry(String serviceCode, String version)throws BifangException{
		List<SoaVersionQos> vq = VersionQosDao.getVersionQos(serviceCode, version);
		for(SoaVersionQos v : vq){
			if(v.getName().equals(EntityConstants.QOS_RETRY)){
				if(v.getValue().equals(EntityConstants.QOS_YES)){
					return true;
				}else if(v.getValue().equals(EntityConstants.QOS_NO)){
					return false;
				}//QOS_DEFAULT
				else{
					List<SoaServiceQos> sq = ServiceQosDao.getServiceQosByServiceCode(serviceCode);
					for(SoaServiceQos s : sq){
						if(s.getValue().equals(EntityConstants.QOS_YES)){
							return true;
						}else {
							return false;
						}
					}
				}
			}
		}
		return false;
		
		
		
	}

}
